home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / gtlayout / source / ltp_glyphsetup.c < prev    next >
C/C++ Source or Header  |  1999-04-19  |  3KB  |  189 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1998 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. /*****************************************************************************/
  15.  
  16. #include <clib/diskfont_protos.h>
  17. #include <pragmas/diskfont_pragmas.h>
  18.  
  19. /*****************************************************************************/
  20.  
  21. #include "Assert.h"
  22.  
  23. /*****************************************************************************/
  24.  
  25. VOID
  26. LTP_GetDefaultFont(struct TTextAttr *TextAttr)
  27. {
  28.     struct RastPort LocalRastPort;
  29.  
  30.     InitRastPort(&LocalRastPort);
  31.  
  32.     memset(TextAttr,0,sizeof(*TextAttr));
  33.  
  34.     AskFont(&LocalRastPort,TextAttr);
  35. }
  36.  
  37.  
  38. /*****************************************************************************/
  39.  
  40.  
  41. struct TextFont *
  42. LTP_OpenFont(struct TextAttr *TextAttr)
  43. {
  44.     struct TextFont *Font;
  45.  
  46.         // Let's see if this one is special
  47.  
  48.     if(TextAttr == (struct TextAttr *)~0)
  49.     {
  50.         struct TTextAttr LocalTextAttr;
  51.  
  52.         memset(&LocalTextAttr,0,sizeof(LocalTextAttr));
  53.  
  54.         Forbid();
  55.  
  56.             // Borrow the system default font data
  57.  
  58.         LTP_GetDefaultFont(&LocalTextAttr);
  59.  
  60.             // Let's hope the font will open...
  61.  
  62.         Font = OpenFont((struct TextAttr *)&LocalTextAttr);
  63.  
  64.         Permit();
  65.     }
  66.     else
  67.     {
  68.         if(!(Font = OpenFont(TextAttr)))
  69.         {
  70.             if(FindTask(NULL)->tc_Node.ln_Type != NT_TASK)
  71.             {
  72.                 struct Library *DiskfontBase;
  73.  
  74.                 if(DiskfontBase = OpenLibrary("diskfont.library",0))
  75.                 {
  76.                     Font = OpenDiskFont(TextAttr);
  77.  
  78.                     CloseLibrary(DiskfontBase);
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.     return(Font);
  85. }
  86.  
  87.  
  88. /*****************************************************************************/
  89.  
  90.  
  91. BOOL
  92. LTP_GlyphSetup(struct LayoutHandle *Handle,struct TextAttr *TextAttr)
  93. {
  94.     struct TextFont *Font;
  95.  
  96.     if(!TextAttr)
  97.         TextAttr = Handle->Screen->Font;
  98.  
  99.     if(Font = LTP_OpenFont(Handle->TextAttr = TextAttr))
  100.     {
  101.         struct RastPort *RPort;
  102.         LONG Count;
  103.         LONG Total;
  104.         ULONG OldStyle;
  105.         LONG GlyphWidth;
  106.         LONG i;
  107.         UBYTE Glyph;
  108.  
  109.         RPort = &Handle->RPort;
  110.  
  111.         if(Handle->CloseFont)
  112.             CloseFont(RPort->Font);
  113.  
  114.         Handle->CloseFont = TRUE;
  115.  
  116.         LTP_SetFont(Handle,Font);
  117.  
  118.         OldStyle = RPort->AlgoStyle;
  119.  
  120.         SetSoftStyle(RPort,FSF_BOLD | FSF_UNDERLINED,FSF_BOLD | FSF_UNDERLINED);
  121.  
  122.             // Cover all printable characters
  123.  
  124.         Count = Total = 0;
  125.  
  126.         for(i = 32 ; i < 256 ; i++)
  127.         {
  128.             if(i == 128)
  129.                 i = 160;
  130.  
  131.             Glyph = i;
  132.  
  133.             GlyphWidth = TextLength(RPort,&Glyph,1);
  134.  
  135.                 // For really pathologic fonts...
  136.  
  137.             if(GlyphWidth >= 0 && GlyphWidth <= 32767)
  138.             {
  139.                 Total += GlyphWidth;
  140.  
  141.                 Count++;
  142.             }
  143.         }
  144.  
  145.         Handle->GlyphWidth = Total / Count;
  146.  
  147.             // Now for the numeric characters
  148.  
  149.         for(Total = 0, i = '0' ; i <= '9' ; i++)
  150.         {
  151.             Glyph = i;
  152.  
  153.             Total += TextLength(RPort,&Glyph,1);
  154.         }
  155.  
  156.         Total /= 10;
  157.  
  158.             // Actually, the numeric character should be
  159.             // just as wide as the space character. Let's
  160.             // hope for the best.
  161.  
  162.         Count = TextLength(RPort," ",1);
  163.  
  164.         if(Total < Count)
  165.             Total = Count;
  166.  
  167.         if(Total > Handle->GlyphWidth)
  168.             Handle->GlyphWidth = Total;
  169.  
  170.         if(Handle->GlyphWidth <= 8)
  171.             Handle->InterWidth = 4;
  172.         else
  173.             Handle->InterWidth = Handle->GlyphWidth / 2;
  174.  
  175.         Handle->GlyphHeight = RPort->TxHeight;
  176.  
  177.         if(Handle->GlyphHeight <= 8)
  178.             Handle->InterHeight = 2;
  179.         else
  180.             Handle->InterHeight = Handle->GlyphHeight / 4;
  181.  
  182.         SetSoftStyle(RPort,OldStyle,FSF_BOLD | FSF_UNDERLINED);
  183.  
  184.         return(TRUE);
  185.     }
  186.     else
  187.         return(FALSE);
  188. }
  189.